package org.jeecg.common.fs;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 文件上传管理
 */
@Component
public class FileServiceManager {
    private final static Logger LG = LoggerFactory.getLogger(FileServiceManager.class);
    private static final Map<String, IFileServiceApapter> serviceAdapters = new ConcurrentHashMap<>();

    /**
     * 系统默认存储方案
     */
    private static String defaultType;


    public FileServiceManager(@Value("${jeecg.fs.defaultType}") String defaultType) {
        FileServiceManager.defaultType = defaultType.toUpperCase();
        LG.info("默认的文件存储方式, defaultType={}", FileServiceManager.defaultType);
    }

    /**
     * 默认附件上传
     * @return
     */
    public static IFileServiceApapter getService(){
        return getService(FileServiceManager.defaultType);
    }

    /**
     * 默认附件上传
     * @return
     */
    public static IFileServiceApapter getlocalService(){
        return getService("LOCAL");
    }

    /**
     * 获取上传服务
     * @param source
     * @return
     */
    public static IFileServiceApapter getService(String source){
        IFileServiceApapter fileServiceApapter = serviceAdapters.get(source);
        return fileServiceApapter;
    }

    /**
     * 注册服务
     * @param source            服务类型
     * @param taskAdapter       服务实现
     */
    public static void registerService(String source, IFileServiceApapter taskAdapter){
        serviceAdapters.put(source.toUpperCase(), taskAdapter);
        LG.info("注册FS服务, type={}, apapter={}", source, taskAdapter);
    }

    public static String getDefaultType(){
        return defaultType;
    }

    public static Boolean isMongoDB(){
        return defaultType.equalsIgnoreCase("MONGODB");
    }
}
